package cn.yuemouren.security.app;

import cn.yuemouren.security.app.authentication.openid.OpenIdAuthenticationSecurityConfig;
import cn.yuemouren.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import cn.yuemouren.security.core.authorize.AuthorizeConfigManager;
import cn.yuemouren.security.core.properties.SecurityConstants;
import cn.yuemouren.security.core.properties.SecurityProperties;
import cn.yuemouren.security.core.validate.code.ValidateCodeSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
 * @Author: Timi
 * @Description: 资源服务器配置
 * @Date: 2020/7/1 21:46
 * @Version: v1.0
 */
@Configuration
@EnableResourceServer
public class TimiResourceServerConfig extends ResourceServerConfigurerAdapter {
    //配置文件获取登录页
    @Autowired
    private SecurityProperties securityProperties;
    //注入数据源
    @Autowired
    private DataSource dataSource;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;
    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;
    @Autowired
    private SpringSocialConfigurer timiSocialConfigurer;
    @Autowired
    private AuthenticationSuccessHandler timiAuthenticationSuccessHandler;
    @Autowired
    private AuthenticationFailureHandler timiAuthenticationFailureHandler;
    @Autowired
    private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;
    @Autowired
    private AuthorizeConfigManager authorizeConfigManager;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                //登录页面
                .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
                //登录请求url
                .loginProcessingUrl(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_FORM)
                //指定成功处理器
                .successHandler(timiAuthenticationSuccessHandler)
                //指定失败处理器
                .failureHandler(timiAuthenticationFailureHandler);

        http.apply(validateCodeSecurityConfig)
                .and()
                .apply(smsCodeAuthenticationSecurityConfig)
                .and()
                .apply(timiSocialConfigurer)
                .and()
                .apply(openIdAuthenticationSecurityConfig)
                .and()
                .userDetailsService(userDetailsService)
                //关闭csrf认证
                .csrf().disable();
        authorizeConfigManager.config(http.authorizeRequests());
    }
}
